package com.shangma.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangma.cn.commons.page.PageBean;
import com.shangma.cn.commons.utils.TreeUtils;
import com.shangma.cn.dto.base.impl.BrandDTO;
import com.shangma.cn.dto.base.impl.MenuDTO;
import com.shangma.cn.entity.base.impl.Brand;
import com.shangma.cn.entity.base.impl.Menu;
import com.shangma.cn.mapper.MenuMapper;
import com.shangma.cn.query.impl.MenuQuery;
import com.shangma.cn.service.MenuService;
import com.shangma.cn.service.base.impl.BaseServiceImpl;
import com.shangma.cn.transfer.base.impl.MenuTransfer;
import com.sun.source.tree.Tree;
import lombok.RequiredArgsConstructor;
import org.jsoup.parser.HtmlTreeBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/12  10:33
 * 文件含义:
 */
@Service
@RequiredArgsConstructor
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {


    public final MenuMapper menuMapper;
    public final MenuTransfer menuTransfer;

    @Override
    public PageBean<MenuDTO> searchPage(MenuQuery menuQuery) {
        PageHelper.startPage(menuQuery.getCurrentPage(), menuQuery.getPageSize());

        if (menuQuery.isQuery()) {
            LambdaQueryWrapper<Menu> lambda = new QueryWrapper<Menu>().lambda();
            if (!StringUtils.isEmpty(menuQuery.getMenuTitle())) {
                lambda.like(Menu::getMenuTitle, menuQuery.getMenuTitle());
            }
            if (menuQuery.getStartTime() != null && menuQuery.getEndTime() != null) {
                lambda.between(Menu::getCreateTime, menuQuery.getStartTime(), menuQuery.getEndTime());
            }

            if (menuQuery.getMenuType() != null) {
                lambda.eq(Menu::getMenuType, menuQuery.getMenuType());
            }
            List<Menu> list = menuMapper.selectList(lambda);
            PageInfo<Menu> pageInfo = new PageInfo<>(list);
            long total = pageInfo.getTotal();
            PageBean<MenuDTO> pageBean = new PageBean<>(total, menuTransfer.toDTOS(list));
            return pageBean;
        } else {
            //获得parentId = 0 的数据
            List<Menu> rootMenus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getParentId, 0));
            PageInfo<Menu> pageInfo = new PageInfo<>(rootMenus);
            long total = pageInfo.getTotal();
            List<Menu> list = menuMapper.selectList(new QueryWrapper<Menu>().lambda().ne(Menu::getParentId, 0));
            List<MenuDTO> menuDTOS = menuTransfer.toDTOS(rootMenus);
            List<MenuDTO> listDTOS = menuTransfer.toDTOS(list);
            TreeUtils.buildTreeData(menuDTOS, listDTOS);
            PageBean<MenuDTO> pageBean = new PageBean<>(total, menuDTOS);
            return pageBean;
        }


    }

    @Override
    public List<MenuDTO> getParentTree() {
        //获得一级的
//        List<Menu> rootMenus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getParentId, 0));
//        List<Menu> list = menuMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getMenuType, 1));
//        List<MenuDTO> menuDTOS = menuTransfer.toDTOS(rootMenus);
//        List<MenuDTO> menuDTOS1 = menuTransfer.toDTOS(list);
//        TreeUtils.buildTreeData(menuDTOS,menuDTOS1);

        //目录和菜单
        List<Menu> dMlist = menuMapper.selectList(new QueryWrapper<Menu>().lambda().ne(Menu::getMenuType, 2));
        //找到了一级
        List<Menu> rootList = dMlist.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
        //把已经找到的一级的删了
        dMlist.removeAll(rootList);

        //在剩余的数据中找寻还租
        List<MenuDTO> menuDTOS = menuTransfer.toDTOS(rootList);
        List<MenuDTO> menuDTOS1 = menuTransfer.toDTOS(dMlist);
        TreeUtils.buildTreeData(menuDTOS, menuDTOS1);

        return menuDTOS;
    }


    @Override
    public int cascadeDeleteChildren(Long id) {
        //子孙删除
        //获取所有子孙id
        List<Long> ids = new ArrayList<>();
        getCascadeChildren(id, ids);
        menuMapper.deleteBatchIds(ids);
        //通过ids批量删除
        return this.deleteById(id);
    }

    @Override
    public List<MenuDTO> getAllMenuTree() {
        List<Menu> menus = menuMapper.selectList(null);
        List<Menu> root = menus.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
        menus.removeAll(root);
        List<MenuDTO> rootDtos = menuTransfer.toDTOS(root);
        List<MenuDTO> menuDTOS = menuTransfer.toDTOS(menus);
        TreeUtils.buildTreeData(rootDtos,menuDTOS);
        return rootDtos;
    }


    public void getCascadeChildren(Long id, List<Long> ids) {
        List<Menu> menus = menuMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getParentId, id));
        if (!CollectionUtils.isEmpty(menus)) {
            ids.addAll(menus.stream().map(Menu::getId).collect(Collectors.toList()));
            menus.forEach(menu -> getCascadeChildren(menu.getId(), ids));
        }
    }


}
